{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #  Count Symmetric Integers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #enumeration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countSymmetricIntegers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计对称整数的数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个正整数 <code>low</code> 和 <code>high</code> 。</p>\n",
    "\n",
    "<p>对于一个由 <code>2 * n</code> 位数字组成的整数 <code>x</code> ，如果其前 <code>n</code> 位数字之和与后 <code>n</code> 位数字之和相等，则认为这个数字是一个对称整数。</p>\n",
    "\n",
    "<p>返回在 <code>[low, high]</code> 范围内的 <strong>对称整数的数目</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>low = 1, high = 100\n",
    "<strong>输出：</strong>9\n",
    "<strong>解释：</strong>在 1 到 100 范围内共有 9 个对称整数：11、22、33、44、55、66、77、88 和 99 。\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>low = 1200, high = 1230\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>在 1200 到 1230 范围内共有 4 个对称整数：1203、1212、1221 和 1230 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= low &lt;= high &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-symmetric-integers](https://leetcode.cn/problems/count-symmetric-integers/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-symmetric-integers](https://leetcode.cn/problems/count-symmetric-integers/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['1\\n100', '1200\\n1230']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isSymmetricInteger(self, n: int) -> bool:\n",
    "        ns: str = \"{:d}\".format(n)\n",
    "        n_digits: int = len(ns)\n",
    "        if n_digits % 2 == 1:\n",
    "            return False\n",
    "\n",
    "        half: int = n_digits // 2\n",
    "        return sum(int(ds) for ds in ns[:half]) == sum(int(ds) for ds in ns[half:])\n",
    "\n",
    "\n",
    "    def countSymmetricIntegers(self, low: int, high: int) -> int:\n",
    "        return sum(\n",
    "            1 if self.isSymmetricInteger(n) else 0\n",
    "            for n in range(low, high + 1)\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSymmetricIntegers(self, low: int, high: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(low,high+1):\n",
    "            if len(str(i))==2:\n",
    "               if str(i)[0] == str(i)[1]:\n",
    "                    ans += 1 \n",
    "            if len(str(i))==4:\n",
    "                if int(str(i)[0])+int(str(i)[1])==int(str(i)[2])+int(str(i)[3]):\n",
    "                    ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "nums = [*range(11, 100, 11)]\n",
    "for i in range(1, 10):\n",
    "    ni = i * 1000\n",
    "    for j in range(10):\n",
    "        k = i + j\n",
    "        n = ni + j * 100\n",
    "        for l in range(10):\n",
    "            if 0 <= k - l <= 9:\n",
    "                nums.append(n + l * 10 + k - l)\n",
    "                \n",
    "class Solution:\n",
    "    def countSymmetricIntegers(self, low: int, high: int) -> int:\n",
    "        return bisect_right(nums, high) - bisect_left(nums, low)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSymmetricIntegers(self, low: int, high: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(low, high + 1):\n",
    "            s = str(i)\n",
    "            n = len(s)\n",
    "            ans += n % 2 == 0 and sum(map(int, s[:n // 2])) == sum(map(int, s[n // 2:]))\n",
    "        return ans\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
